package conultoria.testes;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ExperimentosJFACE {

	public static void main(String args[]) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
//		FormLayout formLayout = new FormLayout();
//		formLayout.marginLeft = 10;
//		formLayout.marginRight = 10;

//		shell.setLayout(formLayout);
		shell.setText("Title da Janela");

		GridLayout gridLay= new GridLayout();
		shell.setLayout(gridLay);

		ListViewer listV =  new ListViewer(shell,SWT.SINGLE);
		GridData gd = new GridData();
		gd.horizontalAlignment = SWT.FILL;
		gd.verticalAlignment = SWT.FILL;
		gd.grabExcessVerticalSpace = true;
		gd.grabExcessHorizontalSpace = true;
		listV.getList().setLayoutData(gd);

		Car[] cars = new Car[5];
		for (int i = 0; i<cars.length; i++) {
			cars[i] = new Car();
			cars[i].setName("Car"+i);
			cars[i].setProprietary(new Proprietary());
			cars[i].getProprietary().setName("Propietary"+i);
			cars[i].setCategory((i%2 == 0)?"Tipo Par":"Tipo Impar");
			cars[i].setColor(100, 100, 100);
		}

		listV.setContentProvider(new IStructuredContentProvider() {

			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				//				System.out.println("input changed");
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public Object[] getElements(Object inputElement) {
				//				System.out.println("Preparando Estrutura de Items");
				return (Object[]) inputElement;
			}
		});
		
		/*LabelProvider
		 * 
		 * INICIO
		 * */
		
		ILabelProvider lProvider = new ILabelProvider() {

			@Override
			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public String getText(Object element) {
				if (element instanceof Car) {
					Car car = (Car) element;
					return car.getName();
				}
				if (element instanceof Proprietary) {
					Proprietary prop = (Proprietary) element;
					return prop.getName();
				}
				return null;
			}

			@Override
			public Image getImage(Object element) {
				return null;
			}
		};

		listV.setLabelProvider(lProvider);
		
		/*FIM*/



		listV.setInput(cars);
		listV.add(cars);

		/*
		 * 
		TREEViewer
		INICIO
		*
		*
		*/
		
		TreeViewer treeV = new TreeViewer(shell, SWT.SINGLE);
		treeV.setContentProvider(new ITreeContentProvider() {

			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				// TODO Auto-generated method stub

			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean hasChildren(Object element) {
//				System.out.println("Tem filho?");
				if (element instanceof Car) {
					Car car = (Car) element;
					if (car.getProprietary() != null) { 
						return true;
					}
				}
				return false;
			}

			@Override
			public Object getParent(Object element) {
				return element;
			}

			@Override
			public Object[] getElements(Object inputElement) {
				return (Object[]) inputElement;
			}

			@Override
			public Object[] getChildren(Object parentElement) {
//				System.out.println("Devolve filhos");
				if (parentElement instanceof Car) {
					Car car = (Car) parentElement;
					return new Object[] {car.getProprietary()};	
				}

				return null;
			}
		});


		treeV.getTree().setLayoutData(gd);
		treeV.setInput(cars);
		treeV.setLabelProvider(lProvider);
		treeV.getTree().setLinesVisible(true);
		
		/*FIM*/
		
		
		/*
		 * 
		TableViewer
		
		INICIO
		*
		*/
		
		final TableViewer  tableV = new TableViewer(shell, SWT.FULL_SELECTION);
		
		TableViewerColumn tvc1 = new TableViewerColumn(tableV, SWT.NONE);
		tvc1.setEditingSupport(new EditingSupport(tvc1.getViewer()) {
			
			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Car) {
					Car car = (Car) element;
					car.setName((String) value);
					tableV.refresh(car);
				}
			}
			
			@Override
			protected Object getValue(Object element) {
				if (element instanceof Car) {
					Car car = (Car) element;
					return car.getName();
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Car)
					return new TextCellEditor(tableV.getTable());
				return null;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				if (element instanceof Car) {
					Car car = (Car) element;
					if (car.getCategory().equals("Tipo Par"))
						return true;
				}
				return false;
			}
		});
		
		TableViewerColumn tvc2 = new TableViewerColumn(tableV, SWT.NONE);
		tvc2.setEditingSupport(new EditingSupport(tvc2.getViewer()) {
			
			ComboBoxCellEditor comboEditor = new ComboBoxCellEditor(tableV.getTable(), new String[]{"Tipo Par", "Tipo Impar"});
			
			@Override
			protected void setValue(Object element, Object value) {
				if(element instanceof Car){
					Car car = (Car) element;
					car.setCategory(comboEditor.getItems()[(Integer) value]);
					tableV.refresh(car);
				}
				
			}
			
			@Override
			protected Object getValue(Object element) {
				if(element instanceof Car){
					Car car = (Car) element;
					
					int index = 0;
					for (String item : comboEditor.getItems()) {
						if (car.getCategory().equals(item)) {
							return index;
						} else {
							index++;
						}
					}
					
					return car.getCategory();
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				if(element instanceof Car){
					return comboEditor;
				}
				return null;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});
		
		
		TableViewerColumn tvc3 = new TableViewerColumn(tableV, SWT.NONE);
		TableViewerColumn tvc4 = new TableViewerColumn(tableV, SWT.NONE);
		tvc4.setEditingSupport(new EditingSupport(tvc4.getViewer()) {
			
			ColorCellEditor colorEditor = new ColorCellEditor(tableV.getTable());
			
			@Override
			protected void setValue(Object element, Object value) {
				if(element instanceof Car){
					Car car = (Car) element;
					RGB rgb = (RGB) value;
					car.setColor(rgb.red, rgb.green, rgb.blue);
					tableV.refresh(car);
				}
				
			}
			
			@Override
			protected Object getValue(Object element) {
				if(element instanceof Car){
					Car car = (Car) element;
					return new RGB(car.getColor()[0], car.getColor()[1], car.getColor()[2]);
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				return colorEditor;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});
		
		
		tableV.getTable().setHeaderVisible(true);
		
//		TextCellEditor columnEditor1 = new TextCellEditor(tableV.getTable());
//		tableV.setCellEditors(new CellEditor[]{columnEditor1, columnEditor1, columnEditor1});
		
		
		tableV.setContentProvider(new IStructuredContentProvider() {

			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				//				System.out.println("input changed");
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public Object[] getElements(Object inputElement) {
				//				System.out.println("Preparando Estrutura de Items");
				return (Object[]) inputElement;
			}
		});
		tableV.setLabelProvider(new ITableLabelProvider(){

			@Override
			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				System.out.println(element+"---->"+property);
				return false;
			}

			@Override
			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public Image getColumnImage(Object element, int columnIndex) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getColumnText(Object element, int columnIndex) {
				if (element instanceof Car) {
					Car car = (Car) element;
					switch(columnIndex) {
						case 0: return car.getName(); 
						case 1: return car.getCategory(); 
						case 2: return car.getProprietary().getName();
						case 3: return car.getColor()[0]+","+car.getColor()[1]+","+car.getColor()[2];
					}
				}
				return null;
			}

		});

		tableV.setInput(cars);
		tableV.getTable().setLayoutData(gd);

		//dimensionar
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableV.getTable().setLayout(tableLayout);

		cars[0].setName("Carro mudado 0");
		cars[1].setName("Carro mudado 1");
		
		tableV.refresh(cars[0]);
		tableV.refresh(cars[1]);
		
		shell.open();

		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}



	}



}
